from django.core.files.storage import Storage
from django.utils.deconstruct import deconstructible
from fdfs_client.client import Fdfs_client
from django.conf import settings
@deconstructible
class FastDFSStorage(Storage):
    def __init__(self,client_conf=None,base_url=None):
        if client_conf is None:
            client_conf=settings.FDFS_CLIENT_CONF
        self.client_conf=client_conf
        if base_url is None:
            base_url=settings.FDFS_URL
        self.base_url=base_url


    def _open(self,name,mode='rb'):
        pass


    def _save(self,name,content):
        client = Fdfs_client(self.client_conf)
        ret=client.upload_by_buffer(content.read())
        if ret.get('Status')!='Upload successed.':
            raise Exception("upload file failed")
        file_name=ret.get('Remote file_id')
        return file_name

    def url(self, name):
        """
        返回文件的完整URL路径
        :param name: 数据库中保存的文件名
        :return: 完整的URL
        """
        return self.base_url + name

    def exists(self, name):
        """
        判断文件是否存在，FastDFS可以自行解决文件的重名问题
        所以此处返回False，告诉Django上传的都是新文件
        :param name:  文件名
        :return: False
        """
        return False